package com.feng.oauth2jwt;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.codec.Codecs;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.jwt.crypto.sign.SignatureVerifier;

import java.util.Map;

/**
 * Created by yfx on 2017-05-26 0026.
 * spring-security-jwt
 */
public class JwtUtils {
    private static Object obj = new Object();
    /**
     * jzq.sso的私钥
     */
    public static String ssoAppPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
            "MIICWwIBAAKBgQCh5Nk2GLiyQFMIU+h3OEA4UeFbu3dCH5sjd/sLTxxvwjXq7JLq\n" +
            "Jbt2rCIdzpAXOi4jL+FRGQnHaxUlHUBZsojnCcHvhrz2knV6rXNogt0emL7f7ZMR\n" +
            "o8IsQGV8mlKIC9xLnlOQQdRNUssmrROrCG99wpTRRNZjOmLvkcoXdeuaCQIDAQAB\n" +
            "AoGAUTcJ1H6QYTOts9bMHsrERLymzir8R9qtLBzrfp/gRxxpigHGLdph8cWmk8dl\n" +
            "N5HDRXmmkdV6t2S7xdOnzZen31lcWe0bIzg0SrFiUEOtg3Lwxzw2Pz0dKwg4ZUoo\n" +
            "GKpcIU6kEpbC2UkjBV4+2E6P1DXuhdgTyHoUA3ycxOdjCAUCQQCyjTzGPXFoHq5T\n" +
            "miJyVd4VXNyCXGU0ZuQayt6nPN8Gd5CcEb2S4kggzPXQcd90FO0kHfZV6+PGTrc2\n" +
            "ZUuz5uwPAkEA6B3lmEmiZsJS/decLzWR0T1CXaFGwTjBQbHXJ0RziAfkuy+VwSmh\n" +
            "vrW/ipk5xbREr5rKx3jVI2PzVOvLw7NgZwJAbUsvDFnH9WfyZZJPy5TsID97awCL\n" +
            "oovozM2phM0p55eAmUfyttp0ND/BqBpMIY49qoH8q5N9FYJRe6Z9tF2B2QJAQBEo\n" +
            "cw039xcB4zCk2l713YQEEmXWarSomuJkWWFKZiyPlJ8Ava0pCMOPl8jNKmWkY7fc\n" +
            "6ovOgJMw8aqXtm+HVwJAerJeUEDez2djG5pIF6aCV0bP3fhQUq8OQCgGF5Qzo9Cn\n" +
            "qvYreGpYKPJGVixAsEPCiLzJRhy1XfFona6VRXIIxw==\n" +
            "-----END RSA PRIVATE KEY-----";
    /**
     * jzq.sso的公钥
     */
    public static String ssoAppPublicKey = "-----BEGIN PUBLIC KEY-----\n" +
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCh5Nk2GLiyQFMIU+h3OEA4UeFb\n" +
            "u3dCH5sjd/sLTxxvwjXq7JLqJbt2rCIdzpAXOi4jL+FRGQnHaxUlHUBZsojnCcHv\n" +
            "hrz2knV6rXNogt0emL7f7ZMRo8IsQGV8mlKIC9xLnlOQQdRNUssmrROrCG99wpTR\n" +
            "RNZjOmLvkcoXdeuaCQIDAQAB\n" +
            "-----END PUBLIC KEY-----";
    /**
     * 签名工具
     */
    private static RsaSigner signer;
    private static RsaVerifier verifier;

    static {
        signer = new RsaSigner(ssoAppPrivateKey);
        verifier = new RsaVerifier(ssoAppPublicKey);
    }

    public byte[] enSign(String data) {
        byte[] content = Codecs.utf8Encode(data);
        byte[] signed = signer.sign(content);
        return signed;
    }

    /**
     * 加密token
     *
     * @param json 要加密的json
     * @return
     */
    public static String encode(JSONObject json) {
        Jwt jwt = JwtHelper.encode(json.toJSONString(), signer);
        return jwt.getEncoded();
    }

    /**
     * @param json    要加密的json
     * @param headers 共享数据,这个可以JwtHelper.headers取得map得取得:{headers.key=headers.val, alg=HS256, typ=JWT}
     * @return
     */
    public static String encode(JSONObject json, Map headers) {
        Jwt jwt = JwtHelper.encode(json.toJSONString(), signer, headers);
        return jwt.getEncoded();
    }

    /**
     * 解密token|token自带了解密串,使用了公私对串再进行一次校验
     *
     * @return
     */
    public static JSONObject decode(String token) throws Exception {
        try {
            Jwt jwt = JwtHelper.decodeAndVerify(token, verifier);
            return JSONObject.parseObject(jwt.getClaims());
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new Exception("校验失败");
        }
    }

    public static void main(String[] args) throws Exception{
//        JSONObject json = new JSONObject();
////        json.put("hehe", "https://my.oschina.net/yifanxiang");
////        json.put("aaa","aaaaa" );
////        json.put("bbb","aaaaa" );
////        String token = JwtUtils.encode(json);
////        System.out.println(token);
////        //Map map=JwtHelper.headers(token);
////        //System.out.println(map);
////        JSONObject info = JwtUtils.decode(token);
////        System.out.println(info);
        System.out.println();//

        Jwt jtw = JwtHelper.decode("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiZGVmYXVsdC1yZXNvdXJjZSJdLCJ1c2VyX25hbWUiOiJtZXJjaGFudCIsInNjb3BlIjpbInJlYWQiXSwiYXRpIjoiMzE1YTRjZmItZGJmMS00YTgwLWE3ZDItZDNkY2FjYWMzOWE5IiwiZXhwIjoxNTUwMzY5Mzk0LCJhdXRob3JpdGllcyI6WyJST0xFX1VTRVIiXSwianRpIjoiN2RlNDRiYjEtNDY1NC00NTliLWIxY2ItNjMzMDg0MDcyMjVmIiwiY2xpZW50X2lkIjoiZjMzZmRmNDVmZjAwMDA1NzZkYzM1MWM0YjIxZWIwYmUifQ.ElOvVIM1h8fh3ZaM7mZVV0VhfYemMtiIuGcQcn8OAyRjK1AaFQ2KWrNJJCOHbKO46hCUzWvwYPS__LKRogbSrz1Ksda4P12LluidjD0R5BRU6Fq84254idP7BCEtcpt980gu7oTEEPYkbimZG-INPIfTMIWrmL3K-Y48G4tQQaI");
        //jtw.verifier(verifySignature);

    }
} 